// pages/home/order_detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bId:"",
    orderVo:"",
    checks: [],
    comment: "匿名评价用户",
    arrayChecks: "",
    userid: "",//下单用户id
    sendUserid: "",//接单回收员id
    orderid: "",
    ifShowV: true,
    ifClick: true,
    ifbuthiddenV: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({ bId: options.bId});
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/serchOrderId.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded;',
      },
      data: {
        bId: that.data.bId,
      },
      success: function (res) {
        let createTime = res.data.orderVo.createTime.substring(0, 16);
        let countPrice = parseFloat(res.data.orderVo.countPrice).toFixed(2);
        res.data.orderVo.createTime = createTime;
        res.data.orderVo.countPrice = countPrice;
        that.setData({ orderVo: res.data.orderVo});
      },
      fail: function (error) {
        console.log("error", error);
      }
    })
  },

  //去定价事件
  toPriced: function (e) {
    var that = this;
    var bId = e.currentTarget.dataset.bid;
    var Type = e.currentTarget.dataset.type;
    var paymentStatus = e.currentTarget.dataset.paymentstatus
    that.setData({ buthiddenPrice: true });

    if (paymentStatus == 2) {
      var countPrice = e.currentTarget.dataset.countprice;
      wx.navigateTo({
        url: 'payment?bId=' + bId + '&allTotal=' + countPrice,
        success: function (res) { that.setData({ buthiddenPrice: false }); },
        fail: function (res) { that.setData({ buthiddenPrice: false }) },
        complete: function (res) { },
      })
    } else {
      if (Type == 3) {
        wx.navigateTo({
          url: 'scan?bId=' + bId,
          success: function (res) { that.setData({ buthiddenPrice: false }); },
          fail: function (res) { that.setData({ buthiddenPrice: false }) },
          complete: function (res) { },
        })
      } else {
        wx.navigateTo({
          url: 'priced?bId=' + bId,
          success: function (res) { that.setData({ buthiddenPrice: false }); },
          fail: function (res) { that.setData({ buthiddenPrice: false }) },
          complete: function (res) { },
        })
      }
    }

  },

  //致电用户事件
  phonecallevent: function (e) {
    var that = this;
    // that.setData({ buthiddenTel: true });
    var tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })

  },
  //取消订单事件
  ifCancel: function (e) {
    var that = this;
    var bId = e.currentTarget.dataset.bid;

    wx.showModal({
      // title: '提示',
      content: '确定取消该订单吗？',
      cancelText: '再等等',
      confirmColor: '#50BEC5',
      success: function (res) {

        if (res.confirm) {
          that.setData({ buthiddenCancel: true });
          wx.request({
            method: "POST",
            url: 'https://www.zaishengwang.com/wx/updateOrderStatus.do',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
              bId: bId,
              status: 3,
            },
            success: function (res) {
              if (res.data.msg != null) {
                that.onOrderInfo();
                that.setData({ buthiddenCancel: false });
              }
            },
            fail: function (error) {
              that.setData({ buthiddenCancel: false });
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },



  changeColor1: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 1,
      checks: [{
        name: "货量少",
        value: '0',
        checked: false
      }, {
        name: "货质一般",
        value: '1',
        checked: false
      }, {
        name: "回收频率低",
        value: '2',
        checked: false
      }, {
        name: "不守信用",
        value: '3',
        checked: false
      }],
      comment: "一般用户"
    });
  },
  changeColor2: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 2,
      checks: [{
        name: "货量少",
        value: '0',
        checked: false
      }, {
        name: "货质一般",
        value: '1',
        checked: false
      }, {
        name: "回收频率低",
        value: '2',
        checked: false
      }, {
        name: "不守信用",
        value: '3',
        checked: false
      }],
      comment: "一般用户"
    });
  },
  changeColor3: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 3,
      checks: [{
        name: "货量较多",
        value: '0',
        checked: false
      }, {
        name: "货质优良",
        value: '1',
        checked: false
      }, {
        name: "回收有频率",
        value: '2',
        checked: false
      }, {
        name: "态度较好",
        value: '3',
        checked: false
      }],
      comment: "优质用户",
    });
  },
  changeColor4: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 4,
      checks: [{
        name: "货量较多",
        value: '0',
        checked: false
      }, {
        name: "货质优良",
        value: '1',
        checked: false
      }, {
        name: "回收有频率",
        value: '2',
        checked: false
      }, {
        name: "态度较好",
        value: '3',
        checked: false
      }],
      comment: "优质用户",
    });
  },
  changeColor5: function () {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 5,
      checks: [{
        name: "货量大",
        value: '0',
        checked: false
      }, {
        name: "货质优",
        value: '1',
        checked: false
      }, {
        name: "回收频率高",
        value: '2',
        checked: false
      }, {
        name: "态度好",
        value: '3',
        checked: false
      }],
      comment: "VIP用户",
    });
  },

  clicks: function (e) {
    let index = e.currentTarget.dataset.index;
    let arrs = this.data.checks;
    let array = new Array;
    let text = e.currentTarget.dataset.text;
    if (arrs[index].checked == false) {
      arrs[index].checked = true;
      array.push(text);
    } else {
      arrs[index].checked = false;
    }
    this.setData({
      checks: arrs
    })
  },

  // 提交清空当前值
  bindSubmit: function () {
    var that = this;
    var checks = that.data.checks;
    var array = new Array;

    for (var i = 0; i < checks.length; i++) {
      if (checks[i].checked == true) {
        array.push(checks[i].name);
      }
    }
    that.setData({ arrayChecks: array })

    if (array.length <= 0) {
      wx.showToast({
        title: '评价类别必选',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    that.setData({ buthiddenV: true });
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toSaveEvaluate.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        userId: that.data.userid,
        sendUserId: that.data.sendUserid,
        orderId: that.data.orderid,
        score: that.data.flag,
        label: that.data.arrayChecks
      },
      success: function (res) {
        var c = 2;
        that.setData({
          buthiddenV: false
        });
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500,
        })
        var intervalId = setInterval(function () {
          c = c - 1;
          if (c == 0) {
            clearInterval(intervalId);
            that.setData({ ifShowV: true, ifClick: true, checks: [], flag: 0, comment: "匿名评价用户" });
          }
        }, 1000)

      },
      fail: function (error) {
      }
    })

  },

  //去评价
  goValuation: function (e) {
    var that = this;
    var userid = e.target.dataset.userid;
    var sendUserid = e.target.dataset.receiptid;
    var orderid = e.target.dataset.ordernumber;
    that.setData({ ifShowV: false, userid: userid, sendUserid: sendUserid, orderid: orderid });
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/serchEvaluateList.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        sendUserId: sendUserid,
        orderId: orderid,
      },
      success: function (res) {
        var length = res.data.evaluateVo.length;
        if (length > 0) {
          that.setData({ ifbuthiddenV: true })
          var score = res.data.evaluateVo[0].score;
          if (score == 1 || score == 2) {
            that.setData({ comment: "一般用户" });
          } else if (score == 3 || score == 4) {
            that.setData({ comment: "优质用户" });
          } else if (score == 5) {
            that.setData({ comment: "VIP用户" });
          }

          var arr = (res.data.evaluateVo[0].label).split(",");
          for (var i = 0; i < arr.length; i++) {

            that.setData({
              ifClick: false,
              checks: that.data.checks.concat([
                {
                  value: i,
                  name: arr[i],
                  checked: true
                }
              ]),
              flag: score
            });

          }

        }

      },
      fail: function (error) {
      }
    })

  },
  //评价隐藏
  cancelV: function () {
    var that = this;
    that.setData({ ifShowV: true, ifClick: true, checks: [], flag: 0, comment: "匿名评价用户" })
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})